package com.ruoyi.buss.dxts.service.impl;

import java.util.*;

import com.ruoyi.buss.utils.ShortMessageServiceSupport;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buss.dxts.mapper.SmSendRecordMapper;
import com.ruoyi.buss.dxts.domain.SmSendRecord;
import com.ruoyi.buss.dxts.service.ISmSendRecordService;

/**
 * 用户登录相关短信Service业务层处理
 * 
 * @author zjh
 * @date 2021-08-02
 */
@Service
public class SmSendRecordServiceImpl implements ISmSendRecordService 
{
    @Autowired
    private SmSendRecordMapper smSendRecordMapper;

    /**
     * 查询用户登录相关短信
     * 
     * @param messageId 用户登录相关短信ID
     * @return 用户登录相关短信
     */
    @Override
    public SmSendRecord selectSmSendRecordById(String messageId)
    {
        return smSendRecordMapper.selectSmSendRecordById(messageId);
    }

    /**
     * 查询用户登录相关短信列表
     * 
     * @param smSendRecord 用户登录相关短信
     * @return 用户登录相关短信
     */
    @Override
    public List<SmSendRecord> selectSmSendRecordList(SmSendRecord smSendRecord)
    {
        return smSendRecordMapper.selectSmSendRecordList(smSendRecord);
    }

    /**
     * 新增用户登录相关短信
     * 
     * @param smSendRecord 用户登录相关短信
     * @return 结果
     */
    @Override
    public int insertSmSendRecord(SmSendRecord smSendRecord)
    {
        smSendRecord.setCreateTime(DateUtils.getNowDate());
        return smSendRecordMapper.insertSmSendRecord(smSendRecord);
    }

    /**
     * 修改用户登录相关短信
     * 
     * @param smSendRecord 用户登录相关短信
     * @return 结果
     */
    @Override
    public int updateSmSendRecord(SmSendRecord smSendRecord)
    {
        return smSendRecordMapper.updateSmSendRecord(smSendRecord);
    }

    /**
     * 批量删除用户登录相关短信
     * 
     * @param messageIds 需要删除的用户登录相关短信ID
     * @return 结果
     */
    @Override
    public int deleteSmSendRecordByIds(String[] messageIds)
    {
        return smSendRecordMapper.deleteSmSendRecordByIds(messageIds);
    }

    /**
     * 删除用户登录相关短信信息
     * 
     * @param messageId 用户登录相关短信ID
     * @return 结果
     */
    @Override
    public int deleteSmSendRecordById(String messageId)
    {
        return smSendRecordMapper.deleteSmSendRecordById(messageId);
    }


    /**
     * 验证码发送
     * @param smSendRecord
     * @return
     */
    @Override
    public Map<String,String> smsSend(SmSendRecord smSendRecord) {
        String phone = null;

        phone = smSendRecord.getPhone();
        if(StringUtils.isNotEmpty(smSendRecord.getUserId())){
            phone = smSendRecordMapper.getUserPhone(smSendRecord.getUserId());
            if(StringUtils.isEmpty(phone)){
                throw new CustomException("当前输入的账户没有绑定手机号或账户不存在!");
            }
            if(!smSendRecord.getPhone().equals(phone)){
                throw new CustomException("输入的手机号和当前账户绑定的手机号不一致!");
            }
        }

        String smType = smSendRecord.getSmType();
        // 生产短信验证码：4位
        String code = generateNumber(4);
        String msg = null;
        int type =Integer.valueOf(smType);
        // 发送短信
        switch (type) {
            case 0:
                msg = "尊敬的客户经理，您好！您正在通过短信登录“效能管理系统”，验证码为：【CODE】。此验证码5分钟内有效，请及时输入，如非本人操作，请忽略此短信。";
                break;
            case 1:
                msg = "尊敬的客户经理，您好！您正在通过短信修改“效能管理系统”的密码，验证码为：【CODE】。此验证码5分钟内有效，请及时输入，如非本人操作，请忽略此短信。";
                break;
            case 2:
                msg = "尊敬的客户经理，您好！您正在通过短信为“效能管理系统”添加绑定设备，验证码为：【CODE】。此验证码5分钟内有效，请及时输入，如非本人操作，请忽略此短信。";
                break;
        }

        msg =msg.replaceAll("【CODE】", code);
        Map<String, Object> result = new ShortMessageServiceSupport().sendShortMessage(phone, msg);
        boolean flag = (boolean) result.get("success");
        if (!flag) {
            throw new CustomException("短信发送失败，请稍后重试！");
        }

        //短信发送发送记录表，并且登陆时判断是否超过5分钟
        SmSendRecord smRecord = new SmSendRecord();
        smRecord.setSmType(smType);
        smRecord.setPhone(phone);
        smRecord.setCode(code);
        smRecord.setContent(msg);
        smRecord.setSendFlag("1");
        smRecord.setCreateTime(new Date());
  /*      homeMapper.saveSms(smRecord);*/
        smSendRecordMapper.insertSmSendRecord(smRecord);

        Map<String,String> data = new HashMap<String,String>();
        data.put("verifyCode", code);
//        data.put("msg", "短信验证码已发送至:"+phone+",请注意查收。");
        return data;

    }

    @Override
    public SmSendRecord vaildSm(SmSendRecord smSendRecord) {
        return smSendRecordMapper.vaildSm(smSendRecord);
    }

    /**
     * 返回一个定长的随机字符串(只包含数字)
     *
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateNumber(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append("0123456789".charAt(random.nextInt("0123456789".length())));
        }
        return sb.toString();
    }
}
